/*
 * node.c
 *
 *  Created on: Nov 28, 2010
 *      Author: cumeo89
 */

#include "util.h"
#include "node.h"
#include "edge.h"
#include <glib/gmem.h>

Node *n_new() {
	Node *node = g_new0(Node, 1);
	return node;
}

void n_free(Node *node) {
	GSList *i;
	for (i = node->edges; i != NULL; i = i->next) {
		Edge *edge = i->data;
		Node *child = edge->child;
		e_free(edge);
		n_free(child);
	}
	g_slist_free(node->edges);
	g_free(node);
}

struct SEdge *n_getParentEdge(Node *node) {
	return node->parentEdge;
}

Node *n_getParent(Node *node) {
	return node->parentEdge->parent;
}

void n_addEdge(Node *node, struct SEdge *edge) {
	if (edge->parent != NULL) {
		n_removeEdge(edge->parent, edge);
	}
	edge->parent = node;
	node->edges = g_slist_append(node->edges, edge);
}

struct SEdge *n_getEdge(Node *node, gunichar c, Ucs4String **stringArr) {
	g_slist_foreach(iter, node->edges) {
		Edge *e = iter->data;
		if (e_charAt(e, 0, stringArr) == c) {
			return e;
		}
	}
	return NULL;
}

void n_removeEdge(Node *node, struct SEdge *edge) {
	node->edges = g_slist_remove(node->edges, edge);
}

gboolean n_isLeaf(Node *node) {
	return node->edges == NULL;
}

